package com.zmc.admin.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.admin.infrastructure.bean.query.SysDictDataQuery;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.admin.infrastructure.dao.SysDictDataDao;
import com.zmc.admin.infrastructure.entity.SysDictDataEntity;
import com.zmc.admin.infrastructure.service.SysDictDataService;


/**
 * @author zmc
 */
@Service("sysDictDataService")
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {

    @Override
    public PageUtils queryPage(SysDictDataQuery query) {
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query)));
    }

    @Override
    public List<SysDictDataEntity> selectDictDataByType(String dictType) {
        return list(getWrapperByDictType(dictType));
    }

    @Override
    public Long countDictDataByType(String dictType) {
        return count(getWrapperByDictType(dictType));
    }

    @Override
    public List<SysDictDataEntity> selectDictDataList(SysDictDataQuery query) {
        return list(getWrapper(query));
    }

    protected QueryWrapper<SysDictDataEntity> getWrapperByDictType(String dictType) {
        QueryWrapper<SysDictDataEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        return wrapper;
    }

    protected LambdaQueryWrapper<SysDictDataEntity> getWrapper(SysDictDataQuery query) {
        LambdaQueryWrapper<SysDictDataEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(query.getDictLabel()), SysDictDataEntity::getDictLabel, query.getDictLabel())
                .eq(StringUtils.isNotEmpty(query.getDictType()), SysDictDataEntity::getDictType, query.getDictType())
                .eq(StringUtils.isNotEmpty(query.getStatus()), SysDictDataEntity::getStatus, query.getStatus())
                .orderByAsc(SysDictDataEntity::getDictSort);
        return wrapper;
    }

}